package com.menusifu.alarm.getway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.security.core.userdetails.MapReactiveUserDetailsService;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @className: UserDetailsServiceAutoConfiguration
 * @copyright: menusifu
 * @description: <一句话功能简述>
 * <功能详细描述>
 * @author: hubin
 * @date: 2021/12/16
 * @version: 1.0
 */
@Configuration
public class UserDetailsServiceAutoConfiguration {

    //定义加密方式
    @Bean
    public  PasswordEncoder passwordEncoder(){
        return  new BCryptPasswordEncoder();
    }

    @Bean
    @Primary
    public MapReactiveUserDetailsService reactiveUserDetailsService(PasswordEncoder passwordEncoder) {
        return new MapReactiveUserDetailsService(innerUser(passwordEncoder));
    }

    //初始化用户名密码
    public List<UserDetails> innerUser(PasswordEncoder passwordEncoder){
        List<UserDetails> userDetails =new ArrayList<>();
       String [][]namePasswords={
             //用户名，密码
               {"hubin","123"},
               {"admin","123"}
       };
        for (String[] namePassword : namePasswords) {
            UserDetails userDetail = User.withUsername(namePassword[0]).password(passwordEncoder.encode(namePassword[1]))
                    .roles(StringUtils.toStringArray(new ArrayList<>())).build();
            userDetails.add(userDetail);
        }
        return userDetails;
    }



}
